package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 虎哥
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> querySpecParams(Long categoryId, Long groupId, Boolean searching) {
        // 1.健壮性判断，不允许前个参数都为空
        if (categoryId == null && groupId == null) {
            // 参数有误
            throw new LyException(400, "分类或规格组参数至少要有一个！");
        }
        // 2.查询，SELECT * FROM tb_spec_param p
        List<SpecParam> list = query()
                //WHERE  p.category_id = 76 AND p.group_id = 1 AND p.searching = 0
                .eq(categoryId!=null, "category_id", categoryId)
                .eq(groupId!=null, "group_id", groupId)
                .eq(searching!=null, "searching", searching)
                .list();
        // 3.返回
        return SpecParamDTO.convertEntityList(list);
    }
}